﻿@using WY.RMS.Domain.Model.Member;
@{
    List<SelectListItem> enableItems = ViewBag.EnableItems;
    Permission AddPermission = ViewBag.AddRoleButton;
    Permission UpdatePermission = ViewBag.UpdateRoleButton;
    Permission DeletePermission = ViewBag.DeleteRoleButton;
    Permission AuthorizePermission = ViewBag.AuthorizeRoleButton;
}
@section css{
    <link href="~/Content/zTree.theme.metro.min.css" rel="stylesheet" />
}
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            角色管理
        </h1>
        <ol class="breadcrumb">
            <li><a href="@Url.Action("Index", "Home", new {area="Common"})"><i class="fa fa-dashboard"></i> 主页</a></li>
            @*<li><a href="#">UI</a></li>*@
            <li class="active">角色管理</li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="panel-body" style="padding-bottom:0px;">
            <div class="panel panel-default">
                <div class="panel-heading">查询条件</div>
                <div class="panel-body">
                    <form id="formSearch" class="form-horizontal">
                        <div class="form-group" style="margin-top: 15px">
                            <label class="control-label  col-sm-1" for="txt_search_rolename">角色名称</label>
                            <div class=" col-sm-3">
                                <input type="text" class="form-control" id="txt_search_rolename">
                            </div>
                            <label class="control-label col-sm-1">是否激活</label>
                            <div class=" col-sm-3">
                                @Html.DropDownList("ddl_enable", enableItems, new { @class = "form-control" })
                            </div>
                            <div class="col-sm-4" style="text-align:right;">
                                <button type="button" style="margin-right:100px" id="btn_query" class="btn btn-primary">查询</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <div id="toolbar" class="btn-group">
                @{
                    if (AddPermission != null)
                    {
                        <button id="btn_add" type="button" class="btn btn-success">
                            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>@AddPermission.Name
                        </button>
                    }
                    if (UpdatePermission != null)
                    {
                        <button id="btn_edit" type="button" class="btn btn-warning">
                            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>@UpdatePermission.Name
                        </button>
                    }
                    if (DeletePermission != null)
                    {
                        <button id="btn_delete" type="button" class="btn btn-danger">
                            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>@DeletePermission.Name
                        </button>
                    }
                    if (AuthorizePermission != null)
                    {
                        <button id="btn_authorize" type="button" class="btn btn-info ">
                            <span class="glyphicon glyphicon-lock" aria-hidden="true"></span>@AuthorizePermission.Name
                        </button>
                    }
                }
            </div>
            <table id="tb_roles"></table>
        </div>

        @Html.Partial("_ModalForm")
        @Html.Partial("AuthorizePermission")
    </section><!-- /.content -->
</div><!-- /.content-wrapper -->
@section  scripts{
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    <script src="~/Scripts/jquery.ztree.all-3.5.min.js"></script>
    <script src="~/Scripts/Custom/TableOperation.js"></script>
    <script src="~/Scripts/Custom/ModalForm.js"></script>
    <script src="~/Scripts/Custom/Role/AuthorizePermissionModal.js"></script>
    <script>
        $(function () {
            //得到表格查询的参数
            var dbQueryParams = function (params) {
                var temp = {   //这里的键的名字和控制器的变量名必须一致，这边改动，控制器也需要改成一样的
                    limit: params.limit,   //页面显示行数
                    offset: params.offset,  //页码*页面显示行数=offset
                    roleName: $("#txt_search_rolename").val(),
                    enable: $("#ddl_enable").val()
                };
                return temp;
            };
            var actionUrl = "@Url.Action("GetRoles")";
            var tbColumns = [
                {
                    checkbox: true
                }, {
                    title: '序号',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return m_pagerow + index + 1;
                    }
                }, {
                    field: 'Id',
                    title: 'Id号',
                    visible: false
                }, {
                    field: 'RoleName',
                    title: '角色名称'
                }, {
                    field: 'Description',
                    title: '描述'
                }, {
                    field: 'StrUpdateDate',
                    title: '更新时间'
                }, {
                    field: 'StrEnabled',
                    title: '是否激活',
                    align: 'center',
                    cellStyle: function (value, row, index) {
                        if (value === '否') {
                            return {
                                css: { "color": "red" }
                            };
                        } else {
                            return {};
                        }
                    }
                }
            ];
            //初始化表格
            InitTable($("#tb_roles"), actionUrl, dbQueryParams, tbColumns);
        });
        //查询角色
        $("#btn_query").click(function () {
            var actionUrl = "@Url.Action("GetRoles")";
            m_pagerow = 0;
            $("#tb_roles").bootstrapTable('refresh', { url: actionUrl });

        });
        //新增角色
        $("#btn_add").click(function () {
            var actionUrl = "@Url.Action("Create")";
            var param = {};
            ShowModal(actionUrl, param, "新增角色");
        });
        //编辑角色
        $("#btn_edit").click(function () {
       
            var arrselections = $("#tb_roles").bootstrapTable('getSelections');
            if (arrselections.length > 1) {
                toastr.warning('只能选择一行进行编辑');
                return;
            }
            if (arrselections.length <= 0) {
                toastr.warning('请选择有效数据');
                return;
            }
            var actionUrl = "@Url.Action("Edit")";
            var param = { id: arrselections[0].Id };
            ShowModal(actionUrl, param, "编辑角色");
        });
        //删除角色
        $("#btn_delete").click(function () {
            var $tb = $("#tb_roles");
            var actionUrl = "@Url.Action("Delete")";
            MultDelete($tb, actionUrl);
        });
        //权限授权
        $("#btn_authorize").click(function () {
            var arrselections = $("#tb_roles").bootstrapTable('getSelections');
            if (arrselections.length > 1) {
                toastr.warning('只能选择一个角色进行授权');
                return;
            }
            if (arrselections.length <= 0) {
                toastr.warning('请选择有效数据');
                return;
            }
            var actionUrl = "@Url.Action("AuthorizePermission")";
            var param = { id: arrselections[0].Id };
            ShowModal_Authorize(actionUrl, param, "权限授权");
        });
        //模态框中“权限授权”保存
        var $modal = $("#authorizeModal");
        $("#btnSave", $modal).click(function () {
            var actionUrl = "@Url.Action("AuthorizePermission")";
            SaveModal_Authorize(actionUrl);
        });
        //模态框中“新增编辑角色”保存
        var $formmodal = $("#modal-form");
        $("#btnSave", $formmodal).click(function () {
            var $tb = $("#tb_roles");
            SaveModal($tb);
        });


    </script>
}

